

#include "MemoryWrapper.h"


MemoryWrapper::MemoryWrapper(StorageType parStorage, IndexingType parIndexing){
	storage = parStorage;
	indexing = parIndexing;

	if (storage == CACHE){
		if (indexing == SIMPLE){
			simpleIndexingCache = new SimpleIndexingCache;
		} else
		if (indexing == COMPLEX){
			cache = new Cache();
		}
	}
	else if (storage == DATABASE){
		database = new Database();
	}
	else if (storage == DISK){
		disk = new Disk();
	}
}


MemoryWrapper::~MemoryWrapper(){
	if (storage == CACHE){
		if (indexing == SIMPLE){
			delete simpleIndexingCache;
		} else
		if (indexing == COMPLEX){
			delete cache;
		}
	}
	else if (storage == DATABASE){
		delete database;
	}
	else if (storage == DISK){
		delete disk;
	}
}


bool MemoryWrapper::insert(PubPkt *pkt, key_t k){
	if (storage == CACHE){
		if (indexing == SIMPLE){
			return simpleIndexingCache->insert(pkt, k);
		} else
		if (indexing == COMPLEX){
			return cache->insert(pkt, k);
		}
	}
	else if (storage == DATABASE){
		return database->insert(pkt, k);
	}
	else if (storage == DISK){
		return disk->insert(pkt, k);
	}
	return false;
}


bool MemoryWrapper::remove(std::pair<timestamp_t, key_t> k){
	if (storage == CACHE){
		if (indexing == SIMPLE){
			return simpleIndexingCache->remove(k);
		} else
		if (indexing == COMPLEX){
			return cache->remove(k);
		}
	}
	else if (storage == DATABASE){
		return database->remove(k);
	}
	else if (storage == DISK){
		return disk->remove(k);
	}
	return false;
}

void MemoryWrapper::extract(int type, Constraint* parCostraint, int parNumConstraint, timestamp_t parEntryTimestamp, float window, OccurrenceType occurrence, std::set<PubPkt*> &resultSet){
	if (storage == CACHE){
		if (indexing == SIMPLE){
			simpleIndexingCache->extract(type, parCostraint, parNumConstraint, parEntryTimestamp, window, occurrence, resultSet);
		} else
		if (indexing == COMPLEX){
			cache->extract(type, parCostraint, parNumConstraint, parEntryTimestamp, window, occurrence, resultSet);
		}
	}
	else if (storage == DATABASE){
		database->extract(type, parCostraint, parNumConstraint, parEntryTimestamp, window, occurrence, resultSet);
	}
	else if (storage == DISK){
		return disk->extract(type, parCostraint, parNumConstraint, parEntryTimestamp, window, occurrence, resultSet);
	}
}
